/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.gacmod.utils;

import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ServiceLoader;
import org.gac.api.genetics.FitnessFunction;

/**
 *
 * @author Tomek
 */
public class PluginServices
{
    private static PluginServices instance = null;    
    public ServiceLoader<FitnessFunction> pluginLoader = null;


    private PluginServices()
    {
        this.pluginLoader = ServiceLoader.load(FitnessFunction.class);
    }

    public static PluginServices getInstance()
    {
        return PluginServices.instance != null ? PluginServices.instance : (PluginServices.instance = new PluginServices());
    }

    public void reload()
    {
        URLClassLoader r;
        
        //this.pluginLoader.getClass().getClassLoader()
        this.pluginLoader.reload();
    }

    public Collection<FitnessFunction> getPlugins()
    {
        Collection<FitnessFunction> result = new ArrayList<FitnessFunction>();

        for (FitnessFunction plugin : pluginLoader)
        {
            result.add(plugin);
        }

        return result;
    }
}
